//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LocalVolCurve.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceCurve.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVarianceCurve^ curve) : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
    CBlackVarianceCurve^ _Ccurve;
    try
    {
#ifdef HANDLE
        _phLocalVolCurve = NULL;
#endif
        _Ccurve = safe_cast<CBlackVarianceCurve^> (curve);
        _Ccurve->Lock();
        Handle<QuantLib::BlackVarianceCurve>& _curve = static_cast<Handle<QuantLib::BlackVarianceCurve>&> (_Ccurve->GetHandle ()); 
        _ppLocalVolCurve = new boost::shared_ptr<QuantLib::LocalVolCurve> (new QuantLib::LocalVolCurve ( _curve ));
        SetLocalVolTermStructure (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurve != nullptr) _Ccurve->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (boost::shared_ptr<QuantLib::LocalVolCurve>& childNative, Object^ owner) : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
#ifdef HANDLE
	_phLocalVolCurve = NULL;
#endif
	_ppLocalVolCurve = &childNative;
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (QuantLib::LocalVolCurve& childNative, Object^ owner) : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
#ifdef HANDLE
	_phLocalVolCurve = NULL;
#endif
	_ppLocalVolCurve = new boost::shared_ptr<QuantLib::LocalVolCurve> (&childNative);
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
    _LocalVolCurveOwner = owner;
    _LocalVolTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (CLocalVolCurve^ copy) : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
#ifdef HANDLE
	_phLocalVolCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLocalVolCurve = new boost::shared_ptr<QuantLib::LocalVolCurve> (copy->GetShared());
        _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (System::Type^ t) : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
#ifdef HANDLE
	_phLocalVolCurve = NULL;
#endif
	if (!t->IsSubclassOf(CLocalVolCurve::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (QuantLib::Handle<QuantLib::LocalVolCurve>& childNative, Object^ owner)  : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
	_phLocalVolCurve = &childNative;
	_ppLocalVolCurve = &static_cast<boost::shared_ptr<QuantLib::LocalVolCurve>>(childNative.currentLink());
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
    _LocalVolCurveOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (QuantLib::Handle<QuantLib::LocalVolCurve> childNative)  : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
	_phLocalVolCurve = &childNative;
	_ppLocalVolCurve = &static_cast<boost::shared_ptr<QuantLib::LocalVolCurve>>(childNative.currentLink());
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::CLocalVolCurve (QuantLib::LocalVolCurve childNative)  : CLocalVolTermStructure(CLocalVolCurve::typeid)
{
#ifdef HANDLE
	_phLocalVolCurve = NULL;
#endif
	_ppLocalVolCurve = new boost::shared_ptr<QuantLib::LocalVolCurve> (new QuantLib::LocalVolCurve (childNative));
    _ppLocalVolTermStructure = new boost::shared_ptr<QuantLib::LocalVolTermStructure> (boost::dynamic_pointer_cast<QuantLib::LocalVolTermStructure> (*_ppLocalVolCurve));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::~CLocalVolCurve ()
{
    if (_ppLocalVolCurve != NULL)
    {
	    delete _ppLocalVolCurve;
        _ppLocalVolCurve = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::!CLocalVolCurve ()
{
    if (_ppLocalVolCurve != NULL)
    {
	    delete _ppLocalVolCurve;
    }
}
QuantLib::LocalVolCurve& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::GetReference ()
{
    if (_ppLocalVolCurve == NULL) throw gcnew NativeNullException ();
	return **_ppLocalVolCurve;
}
boost::shared_ptr<QuantLib::LocalVolCurve>& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::GetShared ()
{
    if (_ppLocalVolCurve == NULL) throw gcnew NativeNullException ();
	return *_ppLocalVolCurve;
}
QuantLib::LocalVolCurve* Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::GetPointer ()
{
    if (_ppLocalVolCurve == NULL) throw gcnew NativeNullException ();
	return &**_ppLocalVolCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LocalVolCurve>& Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::GetHandle ()
{
	if (_phLocalVolCurve == NULL)
	{
		_phLocalVolCurve = new Handle<QuantLib::LocalVolCurve> (*_ppLocalVolCurve);
	}
	return *_phLocalVolCurve;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::HasNative () 
{
	return (_ppLocalVolCurve != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppLocalVolCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppLocalVolCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLocalVolCurve)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLocalVolCurve)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppLocalVolCurve)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Equityfx::ILocalVolCurve^ Cephei::QL::Termstructures::Volatility::Equityfx::CLocalVolCurve_Factory::Create (Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVarianceCurve^ curve)
{
    return gcnew CLocalVolCurve ( curve);
}
